home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Jotto ][ 1.2 / source / Jotto code ƒ / jotto load-save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.4 KB  |  199 lines  |  [TEXT/MMCC]

  1. #include <Script.h>
  2. #include "error.h"
  3. #include "jotto load-save.h"
  4. #include "jotto.h"
  5. #include "jotto environment.h"
  6. #include "file interface.h"
  7. #include "util.h"
  8. #include "graphics.h"
  9. #include "window layer.h"
  10. #include "program globals.h"
  11.  
  12. typedef struct
  13. {
  14.     unsigned short    version;                    /* for forward compatibility */
  15.     unsigned char    numLetters;                    /* 5 or 6 */
  16.     unsigned char    computerWord[6];            /* encrypted! */
  17.     unsigned char    numRight[MAX_TRIES];        /* # letters right in n-th guess */
  18.     unsigned char    humanWord[MAX_TRIES][6];    /* n-th guess */
  19.     unsigned char    numTries;                    /* # guesses so far */
  20.     unsigned char    whichChar;                    /* which char 1-6 of current word */
  21.     unsigned char    checksum;                    /* checksum of previous data */
  22. } SaveStruct;
  23.  
  24. void LoadSaveDispatch(Boolean isLoad, Boolean useOldFile, FSSpec *useThisFS)
  25. {
  26.     FSSpec            saveFile;
  27.     Boolean            alreadyExists;
  28.     enum ErrorTypes    theError;
  29.     WindowPtr        mainWindowPtr;
  30.     Boolean            goon;
  31.     
  32.     if ((!isLoad) && ((mainWindowPtr=GetIndWindowPtr(kMainWindow))==0L))
  33.         return;
  34.     
  35.     if (isLoad)
  36.     {
  37.         if (useOldFile)
  38.         {
  39.             goon=TRUE;
  40.             saveFile=*useThisFS;
  41.         }
  42.         else
  43.         {
  44.             goon=GetSourceFile(&saveFile);
  45.         }
  46.         
  47.         if (goon)
  48.         {
  49.             theError=GetTheFile(&saveFile);
  50.             if (theError!=noErr)
  51.                 HandleError(theError, FALSE);
  52.         }
  53.     }
  54.     else
  55.     {
  56.         saveFile=GetWindowFS(mainWindowPtr);
  57.         useOldFile=useOldFile&&(saveFile.name[0]!=0x00);
  58.         if (!useOldFile)
  59.         {
  60.             if (!GetDestFile(&saveFile, &alreadyExists, "\pSave Jotto ][ game as..."))
  61.                 return;
  62.         }
  63.         
  64.         theError=SaveTheFile(&saveFile, alreadyExists);
  65.         if (theError!=noErr)
  66.             HandleError(theError, FALSE);
  67.     }
  68. }
  69.  
  70. enum ErrorTypes SaveTheFile(FSSpec *saveFile, Boolean alreadyExists)
  71. {
  72.     OSErr            theError;
  73.     short            thisFile;
  74.     long            count;
  75.     SaveStruct        data;
  76.     unsigned char    checksum;
  77.     short            i,j;
  78.     WindowPtr        theWindow;
  79.     
  80.     theWindow=GetIndWindowPtr(kMainWindow);
  81.     
  82.     data.version=SAVE_VERSION;
  83.     data.numLetters=gNumLetters;
  84.     for (i=0; i<6; i++)
  85.         data.computerWord[i]=gComputerWord[i]^0x42;
  86.     for (i=0; i<MAX_TRIES; i++)
  87.         data.numRight[i]=gNumRight[i];
  88.     for (i=0; i<MAX_TRIES; i++)
  89.         for (j=0; j<6; j++)
  90.             data.humanWord[i][j]=gHumanWord[i][j];
  91.     data.numTries=gNumTries;
  92.     data.whichChar=gWhichChar;    
  93.     checksum=0;
  94.     for (i=0; i<sizeof(SaveStruct)-2; i++)
  95.         checksum+=*((unsigned char*)((long)&data+i));
  96.     data.checksum=checksum;
  97.     
  98.     theError=noErr;
  99.     
  100.     if (!alreadyExists)
  101.     {
  102.         theError=FSpCreate(saveFile, CREATOR, SAVE_TYPE, smSystemScript);
  103.         FlushVol(0L, saveFile->vRefNum);
  104.     }
  105.     
  106.     if (theError!=noErr)
  107.         return kCantCreateFile;
  108.     
  109.     FSpCreate(saveFile, CREATOR, SAVE_TYPE, smSystemScript);
  110.     theError=FSpOpenDF(saveFile, fsRdWrPerm, &thisFile);
  111.     
  112.     if (theError!=noErr)
  113.         return kCantOpenFileToSave;
  114.  
  115.     count=sizeof(SaveStruct);
  116.     theError=SetEOF(thisFile, count);
  117.     if (theError!=noErr)
  118.     {
  119.         FSClose(thisFile);
  120.         return kDiskFull;
  121.     }
  122.     
  123.     SetFPos(thisFile, 1, 0L);
  124.     theError=FSWrite(thisFile, &count, &data);
  125.     
  126.     FSClose(thisFile);
  127.     FlushVol(0L, saveFile->vRefNum);
  128.  
  129.     if (theError!=noErr)
  130.     {
  131.         saveFile->name[0]=0x00;
  132.         SetWindowFS(theWindow, *saveFile);
  133.         return kCantWriteFile;
  134.     }
  135.     
  136.     SetWindowFS(theWindow, *saveFile);
  137.     SetWindowIsModified(theWindow, FALSE);
  138.     
  139.     return allsWell;
  140. }
  141.  
  142. enum ErrorTypes GetTheFile(FSSpec *saveFile)
  143. {
  144.     short            thisFile;
  145.     unsigned char    checksum;
  146.     long            count;
  147.     short            i,j;
  148.     SaveStruct        data;
  149.     OSErr            theError;
  150.     WindowPtr        theWindow;
  151.     
  152.     theError=FSpOpenDF(saveFile, fsRdPerm, &thisFile);
  153.     if (theError!=noErr)
  154.         return kCantOpenFileToLoad;
  155.     
  156.     count=sizeof(SaveStruct);
  157.     SetFPos(thisFile, 1, 0L);
  158.     theError=FSRead(thisFile, &count, &data);
  159.     FSClose(thisFile);
  160.     
  161.     if (theError!=noErr)
  162.         return kCantLoadFile;
  163.     
  164.     if (data.version!=SAVE_VERSION)
  165.         return kSaveVersionNotSupported;
  166.     
  167.     checksum=0;
  168.     for (i=0; i<sizeof(SaveStruct)-2; i++)
  169.             checksum+=*((unsigned char*)((long)&data+i));
  170.  
  171.     if (checksum!=data.checksum)
  172.     {
  173.         saveFile->name[0]=0x00;
  174.         return kBadChecksum;
  175.     }
  176.  
  177.     gNumLetters=data.numLetters;
  178.     for (i=0; i<6; i++)
  179.         gComputerWord[i]=data.computerWord[i]^0x42;
  180.     for (i=0; i<15; i++)
  181.         gNumRight[i]=data.numRight[i];
  182.     for (i=0; i<15; i++)
  183.         for (j=0; j<6; j++)
  184.             gHumanWord[i][j]=data.humanWord[i][j];
  185.     gNumTries=data.numTries;
  186.     gWhichChar=data.whichChar;
  187.     if (gNumTries==MAX_TRIES)
  188.         Mymemcpy((Ptr)gHumanWord[gNumTries], (Ptr)gComputerWord, 6);
  189.     
  190.     SetGameInProgress((gNumRight[gNumTries]!=gNumLetters) && (gNumTries<MAX_TRIES));
  191.     StartGame();
  192.     
  193.     theWindow=GetIndWindowPtr(kMainWindow);
  194.     SetWindowFS(theWindow, *saveFile);
  195.     SetWindowIsModified(theWindow, FALSE);
  196.     
  197.     return allsWell;
  198. }
  199.